﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Info
{
    public partial class Frm_AddSupplier : BaseXtraForm
    {
        #region 属性
        public Guid ID { get; set; }
        public event EventHandler SaveEvent;
        byte[][] pByte = new byte[5][];
        List<PictureEdit> pEdit;
        #endregion

        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_AddSupplier(Guid id, EStatus status)
        {
            InitializeComponent();
            this.ID = id;
            this.FStatus = status;
        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_AddSupplier_Load(object sender, EventArgs e)
        {
            if (FStatus == EStatus.Add)
            {
                this.Text = "供应商 - 添加";
            }
            if (FStatus == EStatus.Modify)
            {
                RepastService aservice = new RepastService();
                Supplier supplier = aservice.GetSupplierByID(this.ID);
                this.Text = "供应商 - 编辑";
                EditCode.Text = supplier.Code;
                EditName.Text = supplier.Name;
                EditAddress.Text = supplier.Address;
                EditContracts.Text = supplier.Contracts;
                EditPhone.Text = supplier.Phone;

                ShowSupplierPicture(supplier.ID);
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_OK_Click(object sender, EventArgs e)
        {
            RepastService aservice = new RepastService();
            Supplier supplier = null;
            if (FStatus == EStatus.Add)
            {
                Guid id = aservice.AddSupplier(ID, this.EditCode.Text.Trim(), this.EditName.Text.Trim(), this.EditContracts.Text.Trim(), this.EditPhone.Text.Trim(), this.EditAddress.Text.Trim());
                AddSupplierPicture(id);
                ShowInfo("添加成功!");
            }
            if (FStatus == EStatus.Modify)
            {
                supplier = aservice.UpdateSupplier(ID, this.EditCode.Text.Trim(), this.EditName.Text.Trim(), this.EditContracts.Text.Trim(), this.EditPhone.Text.Trim(), this.EditAddress.Text.Trim());
                aservice.DelSupplierPicture(supplier.ID);
                AddSupplierPicture(supplier.ID);
                ShowInfo("编辑成功");
            }
            if (SaveEvent != null)
            {
                SaveEvent(supplier, new EventArgs());
            }
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 添加供应商图片方法
        /// </summary>
        public void AddSupplierPicture(Guid supplierId)
        {
            var rservice = new RepastService();
            foreach (var item in pByte)
            {
                if (item != null)
                {
                    rservice.AddSupplierPicture(supplierId, item);
                }
            }
        }

        /// <summary>
        /// 编辑状态下显示图片信息
        /// </summary>
        public void ShowSupplierPicture(Guid supplierId)
        {
            var rservice = new RepastService();
            pEdit = new List<PictureEdit> { pictureEdit1, pictureEdit2, pictureEdit3, pictureEdit4, pictureEdit5 };
            var pic = rservice.GetSupplierPicture(supplierId);
            for (int i = 0; i < pEdit.Count; i++)
            {
                if (i >= pic.Count)
                {
                    pEdit[i].EditValue = null;
                    pByte[i] = null;
                }
                else
                {
                    pEdit[i].EditValue = ChangePictureSize(pic[i]);
                    pByte[i] = pic[i];
                }
            }
        }

        /// <summary>
        /// 图片1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureEdit1_MouseClick(object sender, MouseEventArgs e)
        {
            var pic = GetPicture();
            pByte[0] = pic;
            pictureEdit1.EditValue = pic;
        }

        /// <summary>
        /// 图片2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureEdit2_MouseClick(object sender, MouseEventArgs e)
        {
            var pic = GetPicture();
            pByte[1] = pic;
            pictureEdit2.EditValue = pic;
        }

        /// <summary>
        /// 图片3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureEdit3_MouseClick(object sender, MouseEventArgs e)
        {
            var pic = GetPicture();
            pByte[2] = pic;
            pictureEdit3.EditValue = pic;
        }

        /// <summary>
        /// 图片4
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureEdit4_MouseClick(object sender, MouseEventArgs e)
        {
            var pic = GetPicture();
            pByte[3] = pic;
            pictureEdit4.EditValue = pic;
        }

        /// <summary>
        /// 图片5
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureEdit5_MouseClick(object sender, MouseEventArgs e)
        {
            var pic = GetPicture();
            pByte[4] = pic;
            pictureEdit5.EditValue = pic;
        }

        /// <summary>
        /// 打开文件获取图片方法
        /// </summary>
        /// <returns></returns>
        public byte[] GetPicture()
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "打开文件";
            open.ShowHelp = true;
            open.Filter = "图片文件（*.jpg;*.gif;*.bmp;*.png）|*.jpg;*.gif;*.bmp;*.png";
            open.FilterIndex = 1;
            open.RestoreDirectory = false;
            open.InitialDirectory = "C:\\";
            open.Multiselect = false;
            if (open.ShowDialog() == DialogResult.OK)
            {
                return File.ReadAllBytes(open.FileName);
            }
            return null;
        }

        /// <summary>
        /// 改变图片大小方法
        /// </summary>
        public byte[] ChangePictureSize(byte[] data)
        {
            //从流中获取字节
            var img = Image.FromStream(new MemoryStream(data));
            var newimg = new Bitmap(img, new Size(pictureEdit1.Width, pictureEdit1.Height));
            //创建新流
            var ms = new MemoryStream();
            //将图片保存到新流中，并设置格式
            newimg.Save(ms, ImageFormat.Jpeg);
            //将流内容写入字节数组
            return ms.ToArray();
        }

    }
}
